﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Business.Integracoes.Pipedrive
{
    class PipedriveBusiness
    {
        string api_token = "de957b8842b0ad8aa4ccbf50aa8e9cc213e87fd8";

        public bool AdicionaOrganizacao(string pEmpresa, string pTelefone, string pEstado, string pCidade)
        {
            string urlbase = "http://api.pipedrive.com/v1/organizations?api_token=" + api_token;
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlbase);
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"name\":\""+pEmpresa+"\", " +
                    "\"4d569153775bd827131b35777e903bec569e185e\":\"" + pTelefone + "\"," +
                    "\"cdeced95fd0e87e3e251a4b485f7456633022dd2\":\"" + pEstado + "\"," +
                    "\"babf29de2a0dcaddfe58666e8c324e7cfe200cd1\":\"" + pCidade + "\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            var result = "";
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }

            return result.Contains("success");
        }
    }
}
